<!DOCTYPE html>
<html lang="en">
	<head>
		{% include "part_meta.html" %}
		<!-- basic styles -->

		<link href="/static/assets/css/bootstrap.min.css" rel="stylesheet" />
		<link rel="stylesheet" href="/static/assets/css/font-awesome.min.css" />

		<!--[if IE 7]>
		  <link rel="stylesheet" href="/static/assets/css/font-awesome-ie7.min.css" />
		<![endif]-->

		<!-- page specific plugin styles -->

		<!-- fonts -->

		<link rel="stylesheet" href="/static/assets/css/fonts.css" />

		<!-- ace styles -->

		<link rel="stylesheet" href="/static/assets/css/ace.min.css" />
		<link rel="stylesheet" href="/static/assets/css/ace-rtl.min.css" />

		<!--[if lte IE 8]>
		  <link rel="stylesheet" href="/static/assets/css/ace-ie.min.css" />
		<![endif]-->

		<!-- inline styles related to this page -->
        <style>
            em.error {
              background:url("/static/image/unchecked.gif") no-repeat 0px 0px;
              padding-left: 16px;
              color: black;
              font-size: small;
            }
        </style>
		<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->

		<!--[if lt IE 9]>
		<script src="/static/assets/js/html5shiv.js"></script>
		<script src="/static/assets/js/respond.min.js"></script>
		<![endif]-->
	</head>

	<body class="login-layout">
		<div class="main-container">
			<div class="main-content">
				<div class="row">
					<div class="col-sm-10 col-sm-offset-1">
						<div class="login-container">
							<div class="center">
								<h1>
									<i class="icon-leaf green"></i>
									<span class="red">My</span>
									<span class="white">Django</span>
								</h1>
								<h4 class="blue">&copy; 这里输入公司名称</h4>
							</div>

							<div class="space-6"></div>

							<div class="position-relative">
								<div id="login-box" class="login-box visible widget-box no-border">
									<div class="widget-body">
										<div class="widget-main">
											<h4 class="header blue lighter bigger">
												<i class="icon-coffee green"></i>
												请输入您的信息：
											</h4>
                                         {% if form.errors %}
                                          <p class="error" style="color: red;">{{ form.errors.msg }}</p>
                                         {% endif %}
											<div class="space-6"></div>

											<form method="post">
                                                {% csrf_token %}
                                                <input type="hidden" name="next" value="{{ next|escape }}" />
												<fieldset>
													<label class="block clearfix">
														<span class="block input-icon input-icon-right">
															<input name="username" type="text" class="form-control" placeholder="用户名" />
															<i class="icon-user"></i>
														</span>
													</label>

													<label class="block clearfix">
														<span class="block input-icon input-icon-right">
															<input name="password" type="password" class="form-control" placeholder="密码" />
															<i class="icon-lock"></i>
														</span>
													</label>

													<div class="space"></div>

													<div class="clearfix">
														<label class="inline">
															<input type="checkbox" class="ace" />
															<span class="lbl">记住我</span>
														</label>

														<button type="submit" class="width-35 pull-right btn btn-sm btn-primary">
															<i class="icon-key"></i>
															登录
														</button>
													</div>

													<div class="space-4"></div>
												</fieldset>
											</form>

<!-- 											<div class="social-or-login center">
												<span class="bigger-110">Or Login Using</span>
											</div>

											<div class="social-login center">
												<a class="btn btn-primary">
													<i class="icon-facebook"></i>
												</a>

												<a class="btn btn-info">
													<i class="icon-twitter"></i>
												</a>

												<a class="btn btn-danger">
													<i class="icon-google-plus"></i>
												</a>
											</div> -->
										</div><!-- /widget-main -->

										<div class="toolbar clearfix">
											<div>
												<a href="#" onclick="show_box('forgot-box'); return false;" class="forgot-password-link">
													<i class="icon-arrow-left"></i>
忘记密码
												</a>
											</div>

											<div>
												<a href="#" onclick="show_box('signup-box'); return false;" class="user-signup-link">
注册
													<i class="icon-arrow-right"></i>
												</a>
											</div>
										</div>
									</div><!-- /widget-body -->
								</div><!-- /login-box -->

								<div id="forgot-box" class="forgot-box widget-box no-border">
									<div class="widget-body">
										<div class="widget-main">
											<h4 class="header red lighter bigger">
												<i class="icon-key"></i>
												重置密码
											</h4>

											<div class="space-6"></div>
											<p>
												请输入您的验证邮箱并查看：
											</p>

											<form id="forgot_form" method="post" action="{% url 'customer:forgot_pwd' %}">
                                                {% csrf_token %}
												<fieldset>
													<label class="block clearfix">
														<span class="block input-icon input-icon-right">
															<input name="email" id="forgot_email" type="email" class="form-control" placeholder="邮箱" />
															<i class="icon-envelope"></i>
														</span>
													</label>

													<div class="clearfix">
														<button id="send_email" type="submit" class="width-35 pull-right btn btn-sm btn-danger">
															<i class="icon-lightbulb"></i>
															发送!
														</button>
													</div>
												</fieldset>
											</form>
										</div><!-- /widget-main -->

										<div class="toolbar center">
											<a href="#" onclick="show_box('login-box'); return false;" class="back-to-login-link">
												返回登录
												<i class="icon-arrow-right"></i>
											</a>
										</div>
									</div><!-- /widget-body -->
								</div><!-- /forgot-box -->

								<div id="signup-box" class="signup-box widget-box no-border">
									<div class="widget-body">
										<div class="widget-main">
											<h4 class="header green lighter bigger">
												<i class="icon-group blue"></i>
												注册新用户
											</h4>

											<div class="space-6"></div>
											<p id="reg_errors"> 请输入您的详细信息 </p>

											<form id="registerForm" method="post" action="{% url 'customer:register' %}">
                                                {% csrf_token %}
												<fieldset>
													<label class="block clearfix">
														<span class="block input-icon input-icon-right">
															<input name="email" id="reg_email" type="email" class="form-control" placeholder="邮箱"
                                                                   required />
															<i class="icon-envelope"></i>
														</span>
													</label>

													<label class="block clearfix">
														<span class="block input-icon input-icon-right">
															<input name="username" id="reg_username" type="text" class="form-control" placeholder="用户名" />
															<i class="icon-user"></i>
														</span>
													</label>

													<label class="block clearfix">
														<span class="block input-icon input-icon-right">
															<input name="password" id="reg_password" type="password" class="form-control password" placeholder="密码" />
															<i class="icon-lock"></i>
														</span>
													</label>

													<label class="block clearfix">
														<span class="block input-icon input-icon-right">
															<input name="password1" id="reg_password1" type="password" class="form-control" placeholder="重复密码" />
															<i class="icon-retweet"></i>
														</span>
													</label>

													<label class="block">
                                                        <input name="agree" id="reg_agree" type="checkbox" class="ace" checked />
                                                        <span class="lbl">
															我已阅读并同意
															<a href="#">用户协议</a>
														</span>
													</label>

													<div class="space-24"></div>

													<div class="clearfix">
														<button type="button" id="reset" class="width-30 pull-left btn btn-sm">
															<i class="icon-refresh"></i>
															重置
														</button>

														<button type="submit" id="register" class="width-65 pull-right btn btn-sm btn-success">
															注册
															<i class="icon-arrow-right icon-on-right"></i>
														</button>
													</div>
												</fieldset>
											</form>
										</div>

										<div class="toolbar center">
											<a href="#" onclick="show_box('login-box'); return false;" class="back-to-login-link">
												<i class="icon-arrow-left"></i>
												返回登录
											</a>
										</div>
									</div><!-- /widget-body -->
								</div><!-- /signup-box -->
							</div><!-- /position-relative -->
						</div>
					</div><!-- /.col -->
				</div><!-- /.row -->
			</div>
		</div><!-- /.main-container -->

		<!-- basic scripts -->

		<!--[if !IE]> -->

		<script src="/static/assets/js/jquery-2.0.3.min.js"></script>
		<script src="/static/assets/js/jquery.validate.min.js"></script>
		<script src="/static/assets/js/additional-methods.min.js"></script>
        <script src="/static/assets/js/messages_zh.js"></script>
		<!-- <![endif]-->

		<!--[if IE]>
<script src="/static/assets/js/jquery-2.0.3.min.js"></script>
<![endif]-->

		<!--[if !IE]> -->

		<script type="text/javascript">
			window.jQuery || document.write("<script src='/static/assets/js/jquery-2.0.3.min.js'>"+"<"+"/script>");
		</script>

		<!-- <![endif]-->

		<!--[if IE]>
<script type="text/javascript">
 window.jQuery || document.write("<script src='/static/assets/js/jquery-1.10.2.min.js'>"+"<"+"/script>");
</script>
<![endif]-->

		<script type="text/javascript">
			if("ontouchend" in document) document.write("<script src='/static/assets/js/jquery.mobile.custom.min.js'>"+"<"+"/script>");
		</script>

		<!-- inline scripts related to this page -->
		<script type="text/javascript">
			function show_box(id) {
                 jQuery('.widget-box.visible').removeClass('visible');
                 jQuery('#'+id).addClass('visible');
			}

			// 检查邮箱或用户名在服务器是可用或不可用
			function check_reg_info(_name, selecter, check_exists){
                return {
                    type: "post",
                    url: "{% url 'customer:is_exists' %}",
                    dataType: "html",
                    data: {
                        'csrfmiddlewaretoken': $("input[name=csrfmiddlewaretoken]").val(),
                        name: _name,
                        value: function() {
                            return $(selecter).val();  // 这里必须用return来返回值，不然取不到值
                        }
                    },
                    dataFilter: function(data, type) {
                        return data == check_exists;  // 相等时，不触发错误事件
                    }
                }
            }

			$(function () {
                // 参考 http://www.cnblogs.com/si-shaohua/p/3780321.html
                var v = $("#registerForm").validate({
                    rules: {
                        email: {
                            required: true,
                            remote: check_reg_info('email', '#reg_email', 'false')
                        },
                        username: {
                            required: true,
                            minlength: 3,
                            maxlength: 16,
                            remote: check_reg_info('username', '#reg_username', 'false')
                        },
                        password: {
                            required: true,
                            minlength: 3,
                            maxlength: 16
                        },
                        password1: {
                          equalTo: "#reg_password"
                        },
                        agree: {
                            required: true
                        }
                    },
                    messages: {
                        email: {
                            remote: "邮箱已注册"
                        },
                        username: {
                            required: "请输入用户名，3-16个字符（字母、数字、下划线），注册后不能更改",
                            minlength: "用户名长度不能小于3个字符",
                            maxlength: "用户名长度不能大于16个字符",
                            remote: "用户名已注册"
                        },
                        password1: {
                            equalTo: "请输入相同的密码"
                        },
                        agree: {
                            required: "这是必选项"
                        }
                    },
                    submitHandler:function(form){
                        // alert("提交事件!");
                        form.submit();
                        // $(form).ajaxSubmit();
                    },
                    errorElement: "em",
                    errorPlacement: function(error, element) {
                        console.log(error);
                        console.log(element);
                        if (element.is(":radio") )
                            error.appendTo( element.parent().next().next() );
                        else if ( element.is(":checkbox") )
                            error.appendTo ( element.next() );
                        else
                            error.insertAfter(element);
                    }
                });

                $('#reset').on('click', function () {
                    v.resetForm();
                });

                $('#forgot_form').validate({
                    rules: {
                        email: {
                            required: true,
                            remote: check_reg_info('email', '#forgot_email', 'true')
                        }
                    },
                    submitHandler:function(form){
                        // alert("提交事件!");
{#                        form.submit();#}
                        $.post("{% url 'customer:forgot_pwd' %}", $(form).serialize(), function(data){
                            alert('邮件已发送，请登录邮箱去重置密码');
                            console.log(data);
                            show_box("login-box");
                        }, "json");
                    },
                    errorElement: "em",
                    messages: {
                        email: {
                            remote: "邮箱未注册"
                        }
                    }
                });

            });

		</script>
</body>
</html>
